package com.wing.order.service;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.bean.copier.CopyOptions;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wing.order.dao.InventoryOutPlanDetailDao;
import com.wing.order.model.entity.InventoryOutPlanDetail;
import com.wing.order.model.request.InventoryOutPlanDetailForm;
import com.wing.order.model.response.InventoryOutPlanDetailVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.List;
import java.util.Map;

/**
 * 出库单计划明细-ServiceImpl
 *
 * @author
 */
@Service
@Slf4j
public class InventoryOutPlanDetailServiceImpl extends ServiceImpl<InventoryOutPlanDetailDao, InventoryOutPlanDetail> implements InventoryOutPlanDetailService {

    @Autowired
    private InventoryOutPlanDetailDao inventoryOutPlanDetailDao;
    @Override
    public int add(InventoryOutPlanDetailForm inventoryOutPlanDetailForm) {

        //校验

        InventoryOutPlanDetail inventoryOutPlanDetail =new InventoryOutPlanDetail();
        CopyOptions copyOptions = CopyOptions.create()
        .setEditable(InventoryOutPlanDetail.class)
        .setIgnoreError(true)
        .setIgnoreNullValue(true);
        BeanUtil.copyProperties(inventoryOutPlanDetailForm, inventoryOutPlanDetail, copyOptions);

        return baseMapper.insert(inventoryOutPlanDetail);
    }

    @Override
    public List<InventoryOutPlanDetailVO> list4app(Map<String, Object> params) {
        return baseMapper.list4app(params);
    }

    @Override
    public Page<InventoryOutPlanDetailVO> pageList4app(Page page, Map<String, Object> params) {
        List<InventoryOutPlanDetailVO> list = baseMapper.list4app(page, params);
        page.setRecords(list);
        return page;
    }

    @Override
    public InventoryOutPlanDetailVO get4appById(Long id) {
        return baseMapper.get4appById(id);
    }

    @Override
    public List<InventoryOutPlanDetailVO> getInventoryOutPlanDetailVOList(Long planId) {
        List<InventoryOutPlanDetailVO> list = inventoryOutPlanDetailDao.getInventoryOutPlanDetailVOList(planId);
        for (InventoryOutPlanDetailVO item : list){
            item.setTotalMoney(new BigDecimal(item.getQuantity()).multiply(item.getSupplyPrice()).setScale(2,BigDecimal.ROUND_HALF_UP));
        }
        return list;
    }
}
